home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / clients / bitmap / cutpaste.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-12  |  5.8 KB  |  221 lines

  1. /*
  2.  * $XConsortium: CutPaste.c,v 1.6 91/07/24 15:33:34 converse Exp $
  3.  *
  4.  * Copyright 1989 Massachusetts Institute of Technology
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and its
  7.  * documentation for any purpose is hereby granted without fee, provided that
  8.  * the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation, and that the name of M.I.T. not be used in advertising or
  11.  * publicity pertaining to distribution of the software without specific,
  12.  * written prior permission.  M.I.T. makes no representations about the
  13.  * suitability of this software for any purpose.  It is provided "as is"
  14.  * without express or implied warranty.
  15.  *
  16.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  17.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  18.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  21.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  *
  23.  * Author:  Davor Matic, MIT X Consortium
  24.  */
  25.  
  26. #ifdef MSDOS
  27. #include "X11/IntrinsP.h"      /* QDK 05/11/1994 10:44am. */
  28. #else
  29. #include "X11/IntrinsicP.h"
  30. #endif
  31. #include <X11/StringDefs.h>
  32. #include <X11/Xatom.h>
  33. #include "BitmapP.h"
  34.     
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include <math.h>
  38.  
  39. #ifndef abs
  40. #define abs(x)                        (((x) > 0) ? (x) : -(x))
  41. #endif
  42. #define min(x, y)                     (((x) < (y)) ? (x) : (y))
  43. #define max(x, y)                     (((x) > (y)) ? (x) : (y))
  44.  
  45.  
  46. extern Boolean DEBUG;
  47.  
  48. /*****************************************************************************
  49.  *                               Cut and Paste                               *
  50.  *****************************************************************************/
  51.  
  52. /* ARGSUSED */
  53. Boolean ConvertSelection(w, selection, target, type, val_ret, length, format)
  54.     Widget w;
  55.     Atom *selection, *target, *type;
  56.     XtPointer *val_ret;
  57.     unsigned long *length;
  58.     int *format;
  59. {
  60.     caddr_t *value = (caddr_t *)val_ret;
  61.     BitmapWidget BW = (BitmapWidget) w;
  62.     Pixmap *pixmap;
  63.     char *data;
  64.     XImage *image;
  65.     Dimension width, height;
  66.  
  67.     switch (*target) {
  68.  
  69. /*  XA_TARGETS undefined ?!?
  70.  
  71.     case XA_TARGETS:
  72.     *type = XA_ATOM;
  73.     *value = (caddr_t) bitmapClassRec.bitmap_class.targets;
  74.     *length = bitmapClassRec.bitmap_class.num_targets;
  75.     *format = 32;
  76.     return True;
  77.  
  78. */
  79.  
  80.     case XA_BITMAP:
  81.     case XA_PIXMAP:
  82.     if (BWQueryMarked(w)) {
  83.       width = BW->bitmap.mark.to_x - BW->bitmap.mark.from_x + 1;
  84.       height = BW->bitmap.mark.to_y - BW->bitmap.mark.from_y + 1;
  85.       data = CreateCleanData(Length(width, height));
  86.       image = CreateBitmapImage(BW, data, width, height);
  87.       CopyImageData(BW->bitmap.image, image, 
  88.             BW->bitmap.mark.from_x, BW->bitmap.mark.from_y,
  89.             BW->bitmap.mark.to_x, BW->bitmap.mark.to_y, 0, 0);
  90.       pixmap = (Pixmap *) XtMalloc(sizeof(Pixmap));
  91.       *pixmap = GetPixmap(BW, image);
  92.       DestroyBitmapImage(&image);
  93.     }
  94.     else if (BWQueryStored(w)) {
  95.       pixmap = (Pixmap *) XtMalloc(sizeof(Pixmap));
  96.       *pixmap = GetPixmap(BW, BW->bitmap.storage);
  97.     }
  98.     else return False;
  99.     *type = XA_PIXMAP;
  100.     *value = (caddr_t) pixmap;
  101.     *length = 1;
  102.     *format = 32;
  103.     return True;
  104.  
  105.     default:
  106.     return False;
  107.     }
  108. }
  109.  
  110. /* ARGSUSED */
  111. void LoseSelection(w, selection)
  112.     Widget w;
  113.     Atom *selection;
  114. {
  115.     BitmapWidget BW = (BitmapWidget) w;
  116.  
  117.     if (DEBUG)
  118.     fprintf(stderr, "Lost Selection\n");
  119.     BW->bitmap.selection.own = False;
  120.  
  121.     BWUnmark(w);
  122. }
  123.  
  124. /* ARGSUSED */
  125. void SelectionDone(w, selection, target)
  126.     Widget w;
  127.     Atom *selection, *target;
  128. {
  129. /*  Done Automatically ?!?
  130.  
  131.     BitmapWidget BW = (BitmapWidget) w; 
  132.  
  133.     if (*target != XA_TARGETS)
  134.     XtFree(BW->bitmap.value);
  135.  
  136. */
  137. }
  138.  
  139. void BWGrabSelection(w, btime)
  140.     Widget w;
  141.     Time btime;
  142. {
  143.     BitmapWidget BW = (BitmapWidget) w;
  144.  
  145.     BW->bitmap.selection.own = XtOwnSelection(w, XA_PRIMARY, btime,
  146.                           ConvertSelection, 
  147.                           LoseSelection, 
  148.                           SelectionDone);
  149.     if (DEBUG && BW->bitmap.selection.own)
  150.         fprintf(stderr, "Own the selection\n");
  151. }
  152.  
  153. XImage *GetImage();
  154.  
  155. /* ARGSUSED */
  156. void SelectionCallback(w, cldat, selection, type, val, length, format)
  157.     Widget w;
  158.     XtPointer cldat;
  159.     Atom *selection, *type;
  160.     XtPointer val;
  161.     unsigned long *length;
  162.     int *format;
  163. {
  164.     caddr_t value = (caddr_t)val;
  165.     BitmapWidget BW = (BitmapWidget) w;
  166.     Pixmap *pixmap;
  167.  
  168.    switch (*type) {
  169.     
  170.     case XA_BITMAP:
  171.     case XA_PIXMAP:
  172.     DestroyBitmapImage(&BW->bitmap.storage);
  173.     pixmap = (Pixmap *) value;
  174.     BW->bitmap.storage = GetImage(BW, *pixmap);
  175.     XFree((char *)pixmap);
  176.     break;
  177.     
  178.     default:
  179.     XtWarning(" selection request failed.  BitmapWidget");
  180.     break;
  181.     }
  182.  
  183.     BW->bitmap.selection.limbo = FALSE;
  184. }
  185.  
  186. void BWRequestSelection(w, btime, wait)
  187.     Widget w;
  188.     Time btime;
  189.     Boolean wait;
  190. {
  191.   BitmapWidget BW = (BitmapWidget) w;
  192.   
  193.   if (BW->bitmap.selection.own)
  194.     BWStore(w);
  195.   else {
  196.     XtGetSelectionValue(w, XA_PRIMARY, XA_PIXMAP,
  197.             SelectionCallback, NULL, btime);
  198.     
  199.     BW->bitmap.selection.limbo = TRUE;
  200.     
  201.     if (wait)
  202.       while (BW->bitmap.selection.limbo) {
  203.     XEvent event;
  204.     XtNextEvent(&event);
  205.     XtDispatchEvent(&event);
  206.       }
  207.   }
  208. }
  209.  
  210. /* ARGSUSED */
  211. /* Returns true if there is a transferable selection */
  212. Boolean BWQuerySelection(w, btime)
  213.     Widget w;
  214.     Time btime;
  215. {
  216. /* To be written.  XA_TARGETS to be used.  So far undefined ?!? */
  217.  
  218.   return True;
  219. }
  220. /*****************************************************************************/
  221.